/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.util.xml;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author lariverosc
 */
public class NodeXML {

    private String name = "";
    private String value = "";
    private NodeXML parent = null;
    private List<NodeXML> childs = null;

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML() {
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML(String name) {
        this.name = new String(name.replace(' ', '_').replace('/', '-').toLowerCase());
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML(String name, String value) {
        this.name = new String(name.replace(' ', '_').replace('/', '-').toLowerCase());
        this.value = value;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public List<NodeXML> getChilds() {
        return childs;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML getChild(String tag) {
        for (int i = 0; i < childs.size(); i++) {
            if (childs.get(i).name.equalsIgnoreCase(tag.replace(' ', '_').replace('/', '-').toLowerCase())) {
                return childs.get(i);
            }
        }
        return null;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public List<NodeXML> getChilds(String tag) {
        List<NodeXML> desiredChilds = new ArrayList<NodeXML>();
        for (int i = 0; i < childs.size(); i++) {
            if (childs.get(i).name.equalsIgnoreCase(tag.replace(' ', '_').replace('/', '-').toLowerCase())) {
                desiredChilds.add(childs.get(i));
            }
        }
        return desiredChilds;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void setChilds(List<NodeXML> chidls) {
        this.childs = chidls;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void addChild(NodeXML node) {
        if (childs == null) {
            childs = new ArrayList<NodeXML>();
        }
        childs.add(node);
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public String getName() {
        return name;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void setName(String name) {
        this.name = new String(name.replace(' ', '_').replace('/', '-').toLowerCase());
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML getParent() {
        return parent;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void setParent(NodeXML parent) {
        this.parent = parent;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public String getValue() {
        return value;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public boolean isLeaf() {
        if (childs == null) {
            return true;
        } else {
            if (childs.size() == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public boolean hasChild(String tag) {
        if (childs != null) {
            for (int i = 0; i < childs.size(); i++) {
                if (childs.get(i).getName().equalsIgnoreCase(tag.replace(' ', '_').replace('/', '-').toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NodeXML other = (NodeXML) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 37 * hash + (this.value != null ? this.value.hashCode() : 0);
        return hash;
    }
}
